home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_06_05 / v6n5018a.txt < prev    next >
Text File  |  1989-09-26  |  9KB  |  304 lines

  1. /***************************  yref2.y  ****************************/
  2. /*  This version expands the grammar rules so that a more involved
  3.     C source file can be successfully parsed to find identifiers,
  4.     their line number locations, and where they are declared. Use
  5.     "yylex.c" as a test input file, then experiment by making changes 
  6.     in both the input file and the grammar rules. */ 
  7.  
  8.       /* Beginning of declarations section */
  9. %{
  10.          int line_num = 1;
  11.          char identifier[32];
  12. %}
  13.  
  14.       /* Union defined for sending multiple data types from
  15.          yylex() to yyparse() via the reserved internal yacc
  16.          variable yylval. */
  17. %union
  18.     {
  19.     int  char_value;       /* value of single characters */
  20.     char *str_value;       /* value of strings */
  21.     };
  22.  
  23. %token   <str_value> SC_SPEC, TYPE_SPEC, QUAL_SPEC, STRUCT_UNION
  24. %token   <str_value> KEYWORD, IDENTIFIER, ENUM
  25.  
  26. %token   <char_value> STRING CHAR_CONST PRE_PROC OPERATOR NUM_CONST
  27.  
  28. %left ','
  29. %right '='
  30. %left  OPERATOR
  31. %left '*'
  32. %left '(' ')' '[' ']' '.'
  33. %start   source_file
  34.  
  35. %%    /* End of declarations, beginning of grammar rules section. */
  36.  
  37.  
  38. /*  User-defined grammar rules and associated actions go here.  
  39.     The actual rules presented here are a partial specification 
  40.     of the C language grammar, based on the ANSI X3J11 draft 
  41.     (November 9, 1987, Appendix A) for the "external definitions"
  42.     and "declarations" rules, and on K&R (1978, pages 214 to 219)
  43.     for the "statements" and "expressions" rules. Note that the 
  44.     rules presented here have been freely adapted, and often differ
  45.     substantially from the original sources cited.  */ 
  46.  
  47. /* external definitions */
  48. source_file: /* nothing */
  49.          | external_declaration
  50.                 { printf("\n\t\t\t*** Back to top level ***\n"); }
  51.          | source_file external_declaration
  52.                 { printf("\n\t\t\t*** Back to top level ***\n"); }
  53.          ;
  54.  
  55. external_declaration:
  56.            function_definition
  57.          | declaration
  58.          ;
  59.  
  60. function_definition:
  61.            declarator compound_statement
  62.                 { printf("\n\t*** End of function definition -- Type 1 ***"); }
  63.          | declarator declaration_list compound_statement
  64.                 { printf("\n\t*** End of function definition -- Type 2 ***"); }
  65.          | declaration_specifiers declarator compound_statement
  66.                 { printf("\n\t*** End of function definition -- Type 3 ***"); }
  67.          | declaration_specifiers declarator declaration_list compound_statement
  68.                 { printf("\n\t*** End of function definition -- Type 4 ***"); }
  69.          ;
  70.  
  71. /* Declarations */
  72. declaration:
  73.            declaration_specifiers ';'
  74.          | declaration_specifiers init_declarator_list ';'
  75.          ;
  76.  
  77. declaration_specifiers:
  78.            storage_class_specifier 
  79.          | storage_class_specifier declaration_specifiers
  80.          | type_specifier
  81.          | type_specifier declaration_specifiers
  82.          | type_qualifier
  83.          | type_qualifier declaration_specifiers
  84.          ;
  85.  
  86. init_declarator_list:
  87.            init_declarator
  88.          | init_declarator_list ',' init_declarator
  89.          ;
  90.  
  91. init_declarator:
  92.            declarator
  93.          | declarator '=' initializer
  94.          ;
  95.  
  96. initializer:
  97.            expression
  98.          | '{' initializer_list '}'
  99.          | '{' initializer_list ',' '}'
  100.          ;
  101.  
  102. initializer_list:
  103.            initializer
  104.          | initializer_list ',' initializer
  105.          ;
  106.  
  107. storage_class_specifier:
  108.            SC_SPEC
  109.          ;
  110.  
  111. type_specifier:
  112.            TYPE_SPEC
  113.          ;
  114.  
  115. type_qualifier:
  116.            QUAL_SPEC
  117.          ;
  118.  
  119. declarator:
  120.            direct_declarator
  121.          | pointer  direct_declarator
  122.                 { printf(" (Pointer)"); }
  123.          ;
  124.  
  125. direct_declarator:
  126.            IDENTIFIER
  127.                 { printf("\n\tLine %3d: %s  Basic declarator", line_num, $1); }
  128.          | '(' declarator ')'
  129.                 { printf(" -- Parenthesized declarator"); }
  130.          | direct_declarator '[' ']'  %prec '*'
  131.                 { printf(" --  Array declarator 1"); }
  132.          | direct_declarator '[' constant ']' %prec '*' /* Variant */
  133.                 { printf(" --  Array declarator 2"); }
  134.          | direct_declarator '(' parameter_type_list ')' %prec '*'
  135.                 { printf(" --  Function declarator 1"); }
  136.          | direct_declarator '(' ')' %prec '*'
  137.                 { printf(" --  Function declarator 2"); }
  138.          | direct_declarator '(' identifier_list ')' %prec '*'
  139.                 { printf(" --  Function declarator 3"); }
  140.          ;
  141.  
  142. pointer:
  143.            '*'
  144.          | '*' type_specifier_list
  145.          | '*' pointer
  146.          | '*' type_specifier_list pointer
  147.          ;
  148.  
  149. type_specifier_list:
  150.            type_specifier
  151.          | type_specifier_list type_specifier
  152.          ;
  153.  
  154. parameter_type_list:
  155.            parameter_list
  156.                 { printf("\n\tLine %3d: ANSI Function Prototype", line_num); }
  157.          ;
  158.  
  159. parameter_list:
  160.            parameter_declaration
  161.          | parameter_list ',' parameter_declaration
  162.          ;
  163.  
  164. parameter_declaration:
  165.            declaration_specifiers declarator
  166.          | declaration_specifiers 
  167.          | declaration_specifiers abstract_declarator
  168.          ;
  169.  
  170. abstract_declarator:
  171.            pointer
  172.          | direct_abstract_declarator
  173.          | pointer direct_abstract_declarator
  174.          ;
  175.  
  176. direct_abstract_declarator:
  177.            '(' abstract_declarator ')'
  178.          | '[' ']'
  179.          | '[' constant ']' 
  180.          | direct_abstract_declarator '[' ']'
  181.          | direct_abstract_declarator '[' constant ']' 
  182.          | '(' ')'
  183.          | '(' parameter_type_list ')'
  184.          | direct_abstract_declarator '(' ')'
  185.          | direct_abstract_declarator '(' parameter_type_list ')'
  186.          ; 
  187.  
  188. identifier_list:
  189.            IDENTIFIER
  190.                 { printf("\n\tLine %3d: Identifier: %14s", line_num, $1); }
  191.          | identifier_list ',' IDENTIFIER
  192.                 { printf("\n\tLine %3d: Identifier: %14s", line_num, $3); }
  193.          ;
  194.  
  195. /* Statements */
  196. statement:
  197.            compound_statement
  198.          | expression ';'
  199.          | KEYWORD '(' expression ')' statement
  200.          | KEYWORD for_construct statement
  201.          | KEYWORD statement
  202.          | KEYWORD constant ':' statement
  203.          | KEYWORD ':' statement
  204.          | KEYWORD IDENTIFIER ';'
  205.                 { printf("\n\tLine %3d: Identifier: %14s", line_num, $2); }
  206.          | IDENTIFIER ':' statement
  207.                 { printf("\n\tLine %3d: Identifier: %14s", line_num, $1); }
  208.          | ';'
  209.          ;
  210.  
  211. compound_statement:
  212.            '{' '}'
  213.          | '{' declaration_list '}'
  214.          | '{' statement_list '}'
  215.          | '{' declaration_list statement_list '}'
  216.          ;
  217.  
  218. declaration_list:
  219.            declaration
  220.          | declaration_list declaration
  221.          ;
  222.  
  223. statement_list:
  224.            statement
  225.          | statement_list statement
  226.          ;
  227.  
  228. for_construct:
  229.            '(' ';' ';' ')'
  230.          | '(' expression ';' ';' ')'
  231.          | '(' ';' expression ';' ')'
  232.          | '(' ';' ';' expression ')'
  233.          | '(' expression ';' expression ';' ')'
  234.          | '(' ';' expression ';' expression ')'
  235.          | '(' expression ';' expression ';' expression ')'
  236.          ;
  237.  
  238. /* Expressions */
  239. expression:
  240.            primary_expression
  241.          | '*' expression   %prec '*'
  242.          | OPERATOR expression
  243.          | expression OPERATOR
  244.          | expression OPERATOR expression
  245.          | expression '=' expression
  246.          | expression '=' '=' expression  %prec '*'
  247.          | expression '*' expression  %prec OPERATOR
  248.          | expression ',' expression
  249.          ;
  250.  
  251. primary_expression:
  252.            IDENTIFIER
  253.                 { printf("\n\tLine %3d: Identifier: %14s", line_num, $1); }
  254.          | constant
  255.          | STRING
  256.          | '(' expression ')'
  257.          | primary_expression '(' ')'
  258.                 { printf("\n\t\t\t ** End of function call **"); }
  259.          | primary_expression '(' expression ')'
  260.